﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using ConfExpert.Utility.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using ITaskManager.Properties;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ITaskManager.Forms.IT
{
    public partial class Employees : XForm
    {
        public Employees()
        {
            InitializeComponent();
            txtInfo.Caption = "Personel";
        }

        private void Employees_Load(object sender, EventArgs e)
        {

            ((RepositoryItemGridLookUpEdit)gleSirket.Edit).Assign(RI(Rp.Sirket));
            gleSirket.EditValue = 1;
            gleSirket.EditValueChanged += gleSirket_EditValueChanged;
            ((RepositoryItemGridLookUpEdit)gleSirket.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            gvEmployees.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvEmployees_FocusedRowChanged);
            gvEmployees.DisableRowDelete();
            gvEmployees.Init("GetEmployee", WhoAmI, gleSirket.EditValue.ToInt(),false);
            gvEmployees.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gvEmployees.ClearColumnEdit("UserId");

            gvEmployees.SetIdentity("UserId");
            gvEmployees.AddSummary();
            gvEmployees.MakeColumnInvisible("SirketId", "GercekSirketId", "Unvan", "Notes", "KartNo", "Mail1Id", "AXAciklama", "Tckn", "SgkNo", "AmirId", "AxModified", "CEUser");
            gvEmployees.MakeReadOnly("GercekSirketId", "DepartmentId", "TakimId", "Gender", "Username", "Email", "Mobile", "Notes", "KartNo", "Mail1Id", "AXAciklama", "GercekName");
            gvEmployees.RowCellStyle += gvEmployees_RowCellStyle;
            gvEmployees.BestFitColumns();

            GridManager gm = GridManager.GetManager(gvEmployees);
            gm.GridMenu.AddMenu("Kullanıcıyı sil", Sil, Resources.delete_16);
            gm.GridMenu.AddMenu("Şifresini sıfırla", SifreyiSifirla);
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            riHyperLink.SingleClick = false;
            gvEmployees.Columns["Foto"].ColumnEdit = riHyperLink;
            gvEmployees.RowCellClick += gvEmployees_RowCellClick;
            gvEmployees.DataTable().TableNewRow += Employees_TableNewRow;
        }

        void Employees_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["GercekSirketId"] = gleSirket.EditValue.ToInt();
        }


        RepositoryItemHyperLinkEdit riHyperLink = new RepositoryItemHyperLinkEdit();
        void gvEmployees_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (e.Column.FieldName == "Foto")
            {
                DataRow dr = gvEmployees.GetDataRow(e.RowHandle);
                int UserId = dr["UserId"].ToInt();
                if (UserId > 0 &&
                    dr["Foto"].ToStr() != string.Empty)
                {
                    DataTable dtDosya = ExecuteDataTable("GetUsersPhotoByUserId", UserId);

                    byte[] bytes = (byte[])dtDosya.Rows[0]["Photo1"];
                    string FileName = dtDosya.Rows[0]["DosyaGUID"].ToStr() + ".jpg";

                    DevTeamIT.FileManager.RunTempFile(FileName, bytes);
                }
            }

            Cursor.Current = Cursors.Default;
        }

        private void gleSirket_EditValueChanged(object sender, EventArgs e)
        {
            gvEmployees.RefreshDataSource(true, "GetEmployee", WhoAmI, gleSirket.EditValue.ToInt(),false);
            if (gleSirket.EditValue.ToInt() == 2 || gleSirket.EditValue.ToInt() == 5)
                gvEmployees.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            else
                gvEmployees.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

            gvEmployees.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvEmployees.Update("UserId,GercekSirketId,DepartmentId,TakimId,Gender,Username,Email,Mobile,Notes,GercekName", "[User]");
            gvEmployees.VeriyiGuncelle(true, "GetEmployee", WhoAmI, gleSirket.EditValue.ToInt());
        }

        void gvEmployees_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "Name")
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void Sil(object sender, EventArgs e)
        {
            int UserId = gvEmployees.GetFocusedRowCellValue("UserId").ToInt();
            if (UserId > 0)
                if (XtraMessageBox.Show("Emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("UpdDeleteUser", UserId);
                    Notify("Kullanıcı silindi.");
                }
        }

        void SifreyiSifirla(object sender, EventArgs e)
        {
            int UserId = gvEmployees.GetFocusedRowCellValue("UserId").ToInt();
            if (UserId > 0)
                if (XtraMessageBox.Show("Emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("UpdResetPassword", UserId);
                    Notify("Şifre sıfırlandı.");
                }
        }

        void gvEmployees_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dr = gvEmployees.GetDataRow(gvEmployees.FocusedRowHandle);
            txtInfo.Caption = (dr != null ? dr["Name"].ToStr() : string.Empty);
        }


        public object[] SirketId { get; set; }
    }
}

